/**
 * @class   wosh::SessionManager
 * @brief   CoreModule designed to handle users' sessions (wosh::Session).
 *
 * @ingroup CoreModules
 ****************************************************************************
 * @version 0.8.378 $Id: SessionManager.h 2877 2010-08-09 00:02:53Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/*! \page page_core
 * \htmlonly <hr/> \endhtmlonly
 * \section page_core_sessions Sessions
 *
 * A wosh::Session is the (abstract) representation of a communication session,
 * is a semi-permanent interactive information exchange between WOSH Users and the system itself.
 * 
 * Sessions are managed by wosh::SessionManager core-module.
 * 
 * Session implementation may be registered by a system component or the application itself,
 * or created inline by the system (used for instant messaging), its key roles are:
 *
 *  - Parse user's input and interpret it
 *  - Evaluate system events and responses and translate/render for the user
 *
 * Sessions are linked to a wosh::SessionProtocol...
 *
 * \todo [..]
 *
 * wosh::communication::SessionProtocolDefault
 * The most simple session is wosh::communication::SessionShell.
 *
 *
 * \see wosh::SessionManager
 * \see wosh::Session
 * \see wosh::communication
 *
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SessionS;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_SessionManager_H__
 #define __WOSH_Core_SessionManager_H__

 #include <woshDefs.h>
 #include <core/WoshModule.h>

 #include <core/Containers.h>
 #include <core/Session.h>
 #include <core/SessionProtocol.h>
 #include <core/SecurityToken.h>
 #include <core/Fact.h>


namespace wosh {

 #define _SessionManager_NAME						"SessionManager"
 #define _SessionManager_URI						":Sessions"

 #define _SessionManager_KEY_AutoAllocSessionIM		"AutoAllocSessionIM"

 #define _SessionManager_METHOD_ListProtocols		"protocols"
 #define _SessionManager_METHOD_control_session		"control_session"

 #define _SessionManager_EVENT_SessionEvent			"SessionEvent"


class SessionManager : public WoshModule
 {
	WOSH_MODULE(wosh::SessionManager)

	typedef MapT<long, Session*> tLongSessionMap;
	typedef ListT<SessionProtocol*> tSessionProtocolList;


	protected:
		SessionManager( const WoshKernel* parent = NULL );

		WRESULT onKernelInitializing();
		WRESULT onKernelStopping();
		WRESULT onKernelStopped();

		friend class wosh::WoshKernel;

	public:
		virtual ~SessionManager();


//@}
/** @name Public methods
 * @{
 ******************************************************************************/
	public:
		WRESULT registerSession( Session* session_ptr );
		WRESULT unRegisterSession( Session* session_ptr );

//@}
/** @name Communicator Interface
 * @{
 ******************************************************************************/
	public:
		bool isUserReachable( const std::string& username );
		bool isUserRegistered( const std::string& username );
		long getUserLastSeen( const std::string& username );

		WRESULT sendMessageToUser( const std::string& userWosh, const std::string& message );
		WRESULT sendMessageTo( const std::string& userID, const std::string& message );

		WRESULT notify( const Notification* notification );

//@}
/** @name Message Events (incoming message)
 * @{
 ******************************************************************************/
	public:
		void busMessage( const Message& message, const Bus* source );
//@}
/** @name IPropertiesProviderListener interface
 * @{
 ******************************************************************************/
 	protected:
	//	bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );
//@}
/** @name Session Events
 * @{
 ******************************************************************************/
	protected:
		// eval and eventually (un)register session
		// do not eval creation!? a trick?
		void session_event( Session::SESSION_STATE state, Session* source );
		// eval and forward message to bus
		WRESULT session_post_message( Message* message, Session* source );
		// eval and forward call to security manager
		WRESULT session_authorize( const std::string& wosh_username, const SecurityTokenProvider* credentials, Session* source );

		static SessionProtocol* getDefaultProtocol();

		friend class wosh::Session;

//@}
/** @name Internal Mehods and Events management
 * @{
 ******************************************************************************/
	private:
		/**
		 * @brief  Access the SessionManager singleton. Gamma Pattern.
		 *         Static reference is allocated and detroyed by WoshKernel on constructor/destructor respectively
		 * @return pointer to SessionManager singleton. May be NULL (if kernel has not been allocated yet)
		 */
		static SessionManager* getInstance();

		WRESULT registerSession_( Session* session, bool raise_event );
		WRESULT unRegisterSession_( Session* session, bool raise_event );

	//	void evalNotification( const Message& message, const Bus* source );
	//	void forwardMessageForSession( long id, const Message& message, const Bus* source );

//@}
	private:
		tLongSessionMap sessions;			///< map of pairs [id,Session*]
		tSessionProtocolList protocols;		///< list of available protocols, protocols[0]=protocolDefault
		SessionProtocol* protocolDefault;	///< default protocol, allocated in kernel-init

		BusConnector BusSecurity;			///< Bus of Security, network-shared, created by myself on kernel-init

}; // class def


}; // namespace wosh

#endif //__WOSH_Core_SessionManager_H__
